package com.aliengen.thingple.driverapp.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.aliengen.thingple.driverapp.R;
import com.aliengen.thingple.driverapp.data.local.CustomerModel;
import com.aliengen.thingple.driverapp.data.local.PlanGroupModel;
import com.aliengen.thingple.driverapp.data.local.PlanItemModel;
import com.aliengen.thingple.driverapp.data.models.Report;
import com.aliengen.thingple.driverapp.data.models.TruckTag;
import com.aliengen.thingple.driverapp.network.api.ThingpleAPI;
import com.aliengen.thingple.driverapp.network.api.exception.APICallException;
import com.aliengen.thingple.driverapp.ui.fragments.DeliveryPlanListFragment;
import com.aliengen.thingple.driverapp.ui.fragments.YesNoDialogFragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.FragmentById;
import org.androidannotations.annotations.OptionsMenu;

import java.util.List;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
@EActivity(R.layout.activity_customer)
@OptionsMenu(R.menu.login_menu)
public class CustomerActivity extends AbstractBaseActivity implements DeliveryPlanListFragment.OnListFragmentListener {

    @Extra("truck")
    TruckTag truck;

    @FragmentById
    DeliveryPlanListFragment customers_list;

    protected YesNoDialogFragment confirmSelection;

    @AfterViews
    public void init()
    {
        confirmSelection = new YesNoDialogFragment();

        customers_list.getAdapter().updateList(PlanGroupModel.getPlanGroups(truck.getTruckId()));

        try {
            mApplication.api.retrieveDeliveryPlans(truck, new ThingpleAPI.Listener<List<PlanItemModel>>() {
                @Override
                public void onResponse(List<PlanItemModel> customers) {
                    Log.i("CustomerActivity", "Customers retrieved! (" + customers.size() + ")");
                    PlanItemModel.flush();
                    for(PlanItemModel c : customers)
                    {
                        c.save();
                    }
                    customers_list.getAdapter().updateList(PlanGroupModel.getPlanGroups(truck.getTruckId()));
                }

                @Override
                public void onError(String message) {
                    Log.i("CustomerActivity", "Error: " + message);
                    showToast(message);
                }
            });
        } catch(APICallException e)
        {
            e.printStackTrace();
            Log.e("CustomerActivity", e.getMessage());
            showToast(e.getMessage());
        }
    }

    @Override
    public void onClickInteraction(final PlanGroupModel item) {
        String customerName = item.customer.name;

        Log.d("CustomerActivity", "Customer selected: " + customerName);

        Bundle args = new Bundle();
        args.putString("title", getResources().getString(R.string.confirm_customer_selection));
        String message = String.format(getResources().getString(R.string.confirm_customer_message), customerName);
        args.putString("message", message);
        args.putSerializable("customer", item.customer);
        confirmSelection.setArguments(args);

        confirmSelection.setResponse(new YesNoDialogFragment.Response() {
            @Override
            public void onConfirm() {
                Intent nextScreen = new Intent(getApplicationContext(), SelectActionActivity_.class);
                Report report = new Report(truck, item);
                nextScreen.putExtra("report", report);
                startActivity(nextScreen);
                CustomerActivity.this.finish();
            }

            @Override
            public void onCancel() {

            }
        });

        confirmSelection.show(getFragmentManager(), "customerDialog");
    }

    @Override
    public void onLongClickInteraction(PlanGroupModel item) {

    }
}
